/**
 * Created by sky on 2019/3/18.
 */
layui.define(['zepto'], function (exports) {
    var $ = layui.zepto;

    function getParam(paramName) {
        var paramValue = "", isFound = !1;
        if (window.location.search.indexOf("?") == 0 && window.location.search.indexOf("=") > 1) {
            var arrSource = unescape(window.location.search).substring(1, window.location.search.length).split("&"), i = 0;
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
        }
        return paramValue == "" && (paramValue = null), paramValue
    };

    var token = getParam('token');

    var _ajaxRequest = function (option, onSucceedCallback, onErrorCallback) {
        $.ajax({
            headers: {
                "X-AUTH-TOKEN": token, //此处放置请求到的用户token
            },
            data:option.data?JSON.stringify(option.data):{},
            type: option.type,
            url: option.url,
            dataType: option.dataType,
            async: true,
            contentType:'application/json; charset=utf-8',
            success: function (data) {
                if (typeof (onSucceedCallback) == "function") {
                    onSucceedCallback(data);
                }
            },
            error: function (err) {
                if (typeof (onErrorCallback) == "function") {
                    onErrorCallback(err);
                }
            }
        });
    };

    // var apiUrl = 'http://3.1.171.240:8080/'
    var apiUrl = 'http://api.im.zfaya.com:9002/' //183.60.106.190

    var justchatApiMgr = {
        getToken: function (onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"upload/token/",
                type: "GET",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        getURL: function (key, onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"download/token/?keys=" + key,
                type: "GET",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        // 参数4，是否展示详细信息。
        getUserInfo: function (userId, onSucceedCallback, onErrorCallback,verbose) {
            if(verbose){
                var url =  `${apiUrl}users/${userId}/?verbose=true`
            }else{
                var url =  `${apiUrl}users/${userId}/`
            }
            var option = {
                url:url,
                type: "GET",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        // 玩家获得支付账号
        playerGetPayTypes: function (agency_id, onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"pay/pay_types/?agency_id=" + agency_id,
                type: "GET",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
         // 代理获得支付账号
        agentGetPayTypes: function (player_id, onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"pay/pay_types/?player_id=" + player_id,
                type: "GET",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        // 代理下发收款账户
        agentSendReceipt: function (obj, onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"pay/send_receipt/",
                data:{
                    pay_type:Number(obj.pay_type),
                    player_id:Number(obj.player_id)
                },
                type: "POST",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        getQuickReply: function (onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"get_user_quick_reply/",
                type: "GET",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        updateRemark: function (obj, onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"remark/update/",
                data:{
                    target_id:Number(obj.target_id),
                    name:obj.name
                },
                type: "POST",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        pullBlack: function (obj, onSucceedCallback, onErrorCallback) {
            var option = {
                url: apiUrl+"users/blacklist/",
                data:{
                    target_id:Number(obj.target_id),
                    remark:obj.remark
                },
                type: "POST",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        },
        complain: function (obj, onSucceedCallback, onErrorCallback){
            var option = {
                url: apiUrl+"complain/",
                data:obj,
                type: "POST",
                dataType: "json"
            };
            _ajaxRequest(option, onSucceedCallback, onErrorCallback);
        }


    };

    exports('justchatApiMgr', justchatApiMgr);

});
